﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace 餐饮_企划.Models
{

    public class RecipeDB  //实现对数据库表Recipe的增删改查
    {
        MyDbContext myDbContext;
        public RecipeDB(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;//扩大作用域
        }

        // 添加菜谱
        public int Add_Menu(Recipe rec)
        {
            myDbContext.Recipe.Add(rec); // 添加到集合的最后
            //对数据集合Recipe的操作可以有很多次
            int count = myDbContext.SaveChanges();  // 将数据集合中的改变更新到数据表上，达到永久保存的效果

            return count;
        }

        // 获取菜谱
        public Recipe Get_Menu_id(int id)
        {
            Recipe reci = myDbContext.Recipe.FirstOrDefault(r => r.Id == id);
            return reci;
        }

        // 修改菜谱
        public int Edit_Menu(Recipe rec)
        {
            Recipe reci = myDbContext.Recipe.FirstOrDefault(r => r.Id == rec.Id);
            reci.Cai_xi = rec.Cai_xi;
            reci.Cai_name = rec.Cai_name;
            reci.Cai_wen = rec.Cai_wen;
            reci.Add_date = rec.Add_date;
            reci.Zuo_zhe = rec.Zuo_zhe;
            reci.Bei_zhu = rec.Bei_zhu;

            myDbContext.Recipe.Update(reci);
            int count = myDbContext.SaveChanges();

            return count;
        }

        //获取所有菜单
        public List<Recipe> Get_all_Menu()
        {
            List<Recipe> recipes = myDbContext.Recipe.ToList();

            return recipes;
        }


        //删除菜谱
        public bool Delete(int id)
        {
            Recipe reci = myDbContext.Recipe.FirstOrDefault(r => r.Id == id);
            myDbContext.Recipe.Remove(reci);
            int count = myDbContext.SaveChanges();
/*            if (count > 0)
                return true;
            else
                return false;*/
            return count > 0 ? true : false;
        }



        //根据菜名搜索并返回列表
        public List<Recipe> Search(string cai_name)
        {
            return myDbContext.Recipe.Where(r => r.Cai_name.Contains(cai_name)).ToList();
        }

    }

}

